﻿using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using System.Net.Http.Headers;
using System.Net.Http;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using WalkingTec.Mvvm.Admin.Api;
using WalkingTec.Mvvm.Core;
using System.IO;

namespace TyMES.MidWare
{
    // You may need to install the Microsoft.AspNetCore.Http.Abstractions package into your project
    public class AutoLoginMiddleware
    {
        private readonly RequestDelegate _next;
        private readonly IHttpContextAccessor _httpContextAccessor;
        private string remoteToken;

        public AutoLoginMiddleware(RequestDelegate next, IHttpContextAccessor httpContextAccessor)
        {
            _next = next;
            _httpContextAccessor = httpContextAccessor;
        }

        public async Task InvokeAsync(HttpContext context)
        {
            var tokenService = _httpContextAccessor.HttpContext.RequestServices.GetService<ITokenService>();
            var Wtm = _httpContextAccessor.HttpContext.RequestServices.GetService<WTMContext>();
            //if (context.Request.Headers.TryGetValue("appid", out var appId) &&
            //    context.Request.Headers.TryGetValue("appkey", out var appKey))
            //{
            //    //if (appId == "0A60FEB20B16FEB1EE097A17F8DE8306" && appKey == "06BB325425EC6399FA8E525622120B63")
            //    //{
            //    //}

            //    var loginInfo = new SimpleLogin
            //    {
            //        Account = "admin",
            //        Password = "000000",
            //        Tenant = ""
            //    };

            //    var user = Wtm.DoLogin(loginInfo.Account, loginInfo.Password, loginInfo.Tenant);
            //    if (user != null)
            //    {
            //        Wtm.LoginUserInfo = user;
            //        // Assuming the remotetoken is a property of the user object
            //        remoteToken = Wtm.LoginUserInfo.RemoteToken;
            //        context.Request.Headers["Authorization"] = $"Bearer {remoteToken}";
            //    }

            //}
            //else if (context.Request.Path.Value == "/api/BaseInformation/Operation/FMPushOrder"
            //    || context.Request.Path.Value == "/api/MaterialPD/UploadMaterialPD"
            //    || context.Request.Path.Value == "/api/BaseInformation/Station/AutoAdd")
            //{
            //    var loginInfo = new SimpleLogin
            //    {
            //        Account = "admin",
            //        Password = "000000",
            //        Tenant = ""
            //    };

            //    var user = Wtm.DoLogin(loginInfo.Account, loginInfo.Password, loginInfo.Tenant);
            //    if (user != null)
            //    {
            //        Wtm.LoginUserInfo = user;
            //        // Assuming the remotetoken is a property of the user object
            //        remoteToken = Wtm.LoginUserInfo.RemoteToken;
            //        context.Request.Headers["Authorization"] = $"Bearer {remoteToken}";
            //    }
            //}

            var loginInfo = new SimpleLogin
            {
                Account = "admin",
                Password = "000000",
                Tenant = ""
            };

            var user = Wtm.DoLogin(loginInfo.Account, loginInfo.Password, loginInfo.Tenant);
            if (user != null)
            {
                Wtm.LoginUserInfo = user;
                // Assuming the remotetoken is a property of the user object
                remoteToken = Wtm.LoginUserInfo.RemoteToken;
                context.Request.Headers["Authorization"] = $"Bearer {remoteToken}";
            }
            await _next(context);
        }
    }

    // Extension method used to add the middleware to the HTTP request pipeline.
    public static class AutoLoginMiddlewareExtensions
    {
        public static IApplicationBuilder UseAutoLoginMiddleware(this IApplicationBuilder builder)
        {
            return builder.UseMiddleware<AutoLoginMiddleware>();
        }
    }
}
